<?php


namespace app\cms\admin\controller;


use app\cms\dto\carouselInfo;
use app\cms\model\carouselGroupModel;
use app\cms\model\carouselModel;
use iflow\auth\Authorize\AuthAnnotation;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\{ GetMapping, PostMapping, DeleteMapping };

#[Controller('cms/api/admin/carousel')]
class carousel
{
    /**
     * 获取轮播图类目
     * @param string $carousel_group_name
     * @return Response
     * @throws \think\db\exception\DbException
     */
    #[GetMapping]
    #[AuthAnnotation('carousel', 'carouselReadWrite|carouselRead')]
    public function getCarouselGroup(string $carousel_group_name = ''): Response
    {
        $query = carouselGroupModel::where([
            'status' => 1
        ]);
        if ($carousel_group_name !== '') $query -> whereLike('carousel_group_name', "%{$carousel_group_name}%");
        $query = $query -> paginate([
            'page' => request() -> getParams('page'),
            'list_rows' => 10
        ]);
        if ($query -> items()) {
            return message() -> success('查询成功', $query -> items(), [
                'count' => $query -> total(),
                'page' => $query -> lastPage()
            ]);
        }
        return message() -> nodata('暂无数据');
    }

    /**
     * 添加轮播图类目
     * @param string $carousel_group_name
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('carousel', 'carouselReadWrite|carouselWrite')]
    public function addCarouselGroup(string $carousel_group_name = ''): Response
    {
        return message() -> success('添加成功', carouselGroupModel::create([
            'carousel_group_name' => $carousel_group_name
        ]));
    }

    /**
     * 更新轮播图类目
     * @param int $carousel_group_id
     * @param string $carousel_group_name
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('carousel', 'carouselReadWrite|carouselWrite')]
    public function updateCarouselGroup(int $carousel_group_id = 0, string $carousel_group_name = ''): Response
    {
        $update = carouselGroupModel::update([
            'carousel_group_name' => $carousel_group_name
        ], [
            'carousel_group_id' => $carousel_group_id
        ]) -> getChangedData();
        return message() -> success('更新成功', $update);
    }

    /**
     * 删除轮播图类目
     * @param int $carousel_group_id
     * @return Response
     */
    #[DeleteMapping('del_carousel_group/<[0-9]{1,8}:carousel_group_id>')]
    #[AuthAnnotation('carousel', 'carouselReadWrite|carouselWrite')]
    public function delCarouselGroup(int $carousel_group_id = 0): Response
    {
        carouselGroupModel::where([
            'carousel_group_id' => $carousel_group_id
        ]) -> delete();
        carouselModel::where([
            'carousel_group_id' => $carousel_group_id
        ]) -> delete();
        return message() -> success('删除成功');
    }

    /**
     * 添加轮播图
     * @param carouselInfo|null $carouselInfo
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('carousel', 'carouselReadWrite|carouselWrite')]
    public function addCarousel(?carouselInfo $carouselInfo = null): Response
    {
        $arr = (array) $carouselInfo;
        unset($arr['carousel_id']);
        return message() -> success('编辑成功',
            $carouselInfo -> carousel_id === 0?
            carouselModel::create($arr) -> toArray() :
            carouselModel::update($arr, [
                'carousel_id' => $carouselInfo -> carousel_id
            ]) -> getChangedData()
        );
    }

    /**
     * 删除轮播图
     * @param int|null $carousel_id
     * @return Response
     */
    #[DeleteMapping('del_carousel/<[0-9]{1,8}:carousel_id>')]
    #[AuthAnnotation('carousel', 'carouselReadWrite|carouselWrite')]
    public function delCarousel(int $carousel_id = null): Response
    {
        carouselModel::where([
            'carousel_id' => $carousel_id
        ]) -> delete();
        return message() -> success('删除成功');
    }

    /**
     * 更新轮播图
     * @param carouselInfo|null $carouselInfo
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('carousel', 'carouselReadWrite|carouselWrite')]
    public function updateCarousel(?carouselInfo $carouselInfo = null): Response
    {
        if ($carouselInfo !== null) {
            $carousel = (array) $carouselInfo;
            unset($carousel['carousel_id']);
            $update = carouselModel::update($carousel, [
                'carousel_id' => $carouselInfo -> carousel_id
            ]) -> getChangedData();
            return message() -> success('更新成功', $update);
        }
        return message() -> update_failed('更新失败');
    }
}